<?php

	$sex = array("1"=>"男","2"=>"女");
	$state = array("3"=>"启用","4"=>"禁用","5"=>"后台管理员");
	include("./dbconfig.php");
	$link = @mysqli_connect(HOST,USER,PASS) or die("数据库连接失败");
	mysqli_select_db($link,DBNAME);
	mysqli_set_charset($link,"utf8");
	
	switch($_GET['a']){ //添加
		case 'insert':
            $username = $_POST['username'];
			$name = $_POST['name'];
            $pass = $_POST['pass'];
            $sex = $_POST['sex'];
            $address = $_POST['address'];
            $code = $_POST['code'];
            $phone = $_POST['phone'];
            $email = $_POST['email'];
            $state = $_POST['state'];
            $addtime = time();
            
			
			$sql = "insert into users(username,name,pass,sex,address,code,phone,email,state,addtime) values('$username','$name','$pass','$sex','$address','$code','$phone','$email','$state','$addtime')";
			$result = mysqli_query($link,$sql);
			
            if(mysqli_insert_id($link)>0){
                header("Location:index.php");
            }else{
                echo "添加失败";
            }
           echo mysqli_error($link);
		break;

        case 'del': //删除
            $id = $_GET['id'];
            $sql = "delete from users where id={$id}";
            
            $result = mysqli_query($link,$sql);
            
            if(mysqli_affected_rows($link)>0){
                header("Location:index.php");
            }else{
                echo "删除失败";
            }
            
        break;
        
        case 'update': //修改
            $username = $_POST['username'];
			$name = $_POST['name'];
            $pass = $_POST['pass'];
            $sex = $_POST['sex'];
            $address = $_POST['address'];
            $code = $_POST['code'];
            $phone = $_POST['phone'];
            $email = $_POST['email'];
            $state = $_POST['state'];
            $addtime = $_POST['addtime'];
           
            $sql = "update users set username='$username',name='$name',pass='$pass',sex='$sex',address='$address',code='$code',phone='$phone',email='$email',state='$state',addtime='$addtime' where id={$_POST['id']}";
            
            $result = mysqli_query($link,$sql);
            
           
                header("Location:index.php");
            
        break;
	}